import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
    children: [
      {
        path: "/home",
        redirect: '/home/page'
      },
      {
        path: '/home/page',
        name: 'Page',
        component: () => import(/* webpackChunkName: "page" */ '../views/Page.vue')
      },
      {
        path: '/home/list',
        name: 'List',
        component: () => import(/* webpackChunkName: "list" */ '../views/List.vue')
      }, {
        path: '/home/profile',
        name: 'Profile',
        component: () => import(/* webpackChunkName: "profile" */ '../views/Profile.vue')
      }]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '/rent',
    name: 'Rent',
    component: () => import(/* webpackChunkName: "rent" */ '../views/Rent.vue'),
  }, {
    path: '/rent/add',
    name: 'Add',
    component: () => import(/* webpackChunkName: "add" */ '../views/Add.vue'),
  }, {
    path: '/map',
    name: 'Map',
    component: () => import(/* webpackChunkName: "map" */ '../views/Map.vue'),
  }, {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import(/* webpackChunkName: "detail" */ '../views/Detail.vue'),
  },
  {
    path: '/citylist',
    name: 'Citylist',
    component: () => import(/* webpackChunkName: "citylist" */ '../views/Citylist.vue'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.path === '/home/profile' && !localStorage.getItem('username')) {
    next('/login')
  }
  next()
})

export default router
